package com.jrj.util;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 *@author Adminstrator
 *@version createTime��2011-11-14 04:39:44
 */
public class EncodingInterceptor extends AbstractInterceptor {
	
	
		
//	private static final String IE_CONTENT_TYPE = "application/x-www-form-urlencoded";  
//    private static final String FF_AJAX_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=UTF-8";  
//	private static final String XMLHTTP_REQUEST = "XMLHttpRequest";
//	private static final String AJAX_CHARACTER_ENCODING_UTF8 = "utf-8";
	
	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		ActionContext actionContext = arg0.getInvocationContext();   
		HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST); 
		
//		String requestedWith = request.getHeader("x-requested-with");  
//        String type = request.getHeader("Content-Type");
//        String charSet = request.getHeader("Accept-Charset");
		
		if( request.getMethod().compareToIgnoreCase("post")>=0){
		      try {
		       request.setCharacterEncoding("UTF-8");
		      } catch (UnsupportedEncodingException e) {
		       // TODO Auto-generated catch block
		       e.printStackTrace();
		      }
		     }else{
		            
		      Iterator iter=request.getParameterMap().values().iterator();
		      while(iter.hasNext())
		      {
		       String[] parames=(String[])iter.next();
		       for (int i = 0; i < parames.length; i++) {
		        try {
		         parames[i]=new String(parames[i].getBytes("iso8859-1"),"GBK");//�˴�GBK��ҳ�����һ��
		        } catch (UnsupportedEncodingException e) {
		         e.printStackTrace();
		        }
		       }   
		      }   
		       }
		      return arg0.invoke();
	}

}
